package com.yihubang.taobaokotlin.ui.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.yihubang.taobaokotlin.model.Categories
import com.yihubang.taobaokotlin.ui.fragment.HomePagerFragment

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/10.
 * PS: Not easy to write code, please indicate.
 */
class HomePagerAdapter(fragmentManager: FragmentManager) : FragmentPagerAdapter(
    fragmentManager,
    BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
) {

    val categories = mutableListOf<Categories.DataBean>()

    override fun getPageTitle(position: Int): CharSequence? {
        return categories[position].title
    }

    override fun getItem(position: Int): Fragment {
        return HomePagerFragment.newInstance(categories[position])
    }

    override fun getCount() = categories.size

    fun setCategories(categories: Categories?) {
        val datas = categories?.data
        this.categories.clear()
        this.categories.addAll(datas!!)
        notifyDataSetChanged()
    }
}